package cn.jwcat.controller;

import cn.jwcat.dealEntity.GoodsDto;
import cn.jwcat.entity.Ipaddress;
import cn.jwcat.entity.Login;
import cn.jwcat.entity.Notice;
import cn.jwcat.service.IGoodsService;
import cn.jwcat.service.IIpaddressService;
import cn.jwcat.service.INoticeService;
import cn.jwcat.service.IOtherService;
import cn.jwcat.utils.result.Result;
import cn.jwcat.utils.result.ResultCodeEnum;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 登录相关操作
 */
@RestController
public class OtherController {
	@Resource
	IOtherService otherService;

	@Resource
	IIpaddressService ipaddressService;
	@Resource
	INoticeService noticeService;
	@Resource
	IGoodsService goodsService;

	/**
	 * 登录操作
	 */
	@PostMapping("/login")
	@Transactional
	@ApiOperation("登录成功，返回TOKEN")
	public Result login(@RequestBody Login login) throws Exception {
		return otherService.login(login);
	}

	@GetMapping("/ip")
	@ApiOperation("获取ip列表")
	public Result getIp() throws Exception {
		List<Ipaddress> list = ipaddressService.list();
		return Result.success(list);
	}

	/**
	 * 获取登录用户的信息
	 */
	@GetMapping("/info")
	@ApiOperation("根据token获取用户的详细信息")
	public Result getInfo(HttpServletRequest request) {
		return otherService.getInfo(request);
	}

	/**
	 * 退出登录操作
	 */
	@PostMapping("/logout")
	@ApiOperation("退出登录")
	public Result logout() {
		return Result.api(ResultCodeEnum.USER_LOGOUT_SUCCESS);
	}

	@GetMapping("app/notice/last")
	@ApiOperation("获取公告的前六条数据")
	public Result getNoticeLastSix() {
		List<Notice> notices = noticeService.getNoticeLastSix();
		return Result.success(notices);
	}

	@GetMapping("app/goods/last")
	@ApiOperation("获取物资的前六条数据")
	public Result getGoodsLastSix() {
		List<GoodsDto> goodsDtos = goodsService.getGoodsLastSix();
		return Result.success(goodsDtos);
	}

	@PostMapping("app/login")
	@ApiOperation("患者登录")
	public Result userLogin(@RequestBody Login login) {
		return otherService.userLogin(login);
	}
}
